package com.ktwlsoft.archivemanage.controller;

import com.ktwlsoft.archivemanage.domain.business.businessService.TransferService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.OperationRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.OperationLog;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.operationLog.SaveLogRequest;
import com.ktwlsoft.archivemanage.payload.transfer.ArchivesTransferRequest;
import com.ktwlsoft.archivemanage.payload.transfer.CacnelTransferRequest;
import com.ktwlsoft.archivemanage.service.security.UserAuthenticate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.Year;

/**
 * 档案移交
 */
@RestController
@RequestMapping("/transfer")
@Api(description = "档案移交")
public class TransferController {

    private final TransferService transferService;

    public TransferController(TransferService transferService) {
        this.transferService = transferService;
    }

    @ApiOperation("列表")
    @GetMapping("/list")
    @UserAuthenticate(actionCode = "TRANSFER_LIST", skip = true)
    public ResponseEntity<?> transferList(int pn, int pageSize, String keywords, Boolean transfered) {
        return ResponseEntity.ok(Result.ok(transferService.findByPage(pn, pageSize, keywords, transfered)));
    }

    @ApiOperation("详情")
    @GetMapping("/detail")
    @UserAuthenticate(actionCode = "TRANSFER_DETAIL", skip = true)
    public ResponseEntity<?> transferDetail(@RequestParam Long unitId) {
        return ResponseEntity.ok(Result.ok(transferService.findDetail(unitId)));
    }

    @ApiOperation("详情列表")
    @GetMapping("/detailList")
    @UserAuthenticate(actionCode = "TRANSFER_DETAIL_LIST", skip = true)
    public ResponseEntity<?> transferDetailList(int pn, int pageSize, @RequestParam Long unitId, @RequestParam String field, @RequestParam String order) {
        return ResponseEntity.ok(Result.ok(transferService.findDetailList(pn, pageSize, unitId, field, order)));
    }

    @ApiOperation("档案移交")
    @PostMapping("/transferArchives")
    @UserAuthenticate(actionCode = "TRANSFER_TRANSFER_CONFIRM")
    public ResponseEntity<?> transferArchives(@RequestBody ArchivesTransferRequest request) {
        transferService.transferArchives(request);
        OperationRepositoryService.saveLog(new SaveLogRequest(
                OperationLog.OperationType.TRANSFER,
                request.getArchiveUnit() + "移交了" + request.getYear() + "年度档案",
                "档案移交",
                OperationLog.BusinessType.BUSINESS
        ));
        return ResponseEntity.ok(Result.ok("移交完成"));
    }

    @ApiOperation("撤销移交")
    @PostMapping("/cancelTransfer")
    @UserAuthenticate(actionCode = "TRANSFER_TRANSFER_CANCEL")
    public ResponseEntity<?> cancelTransfer(@RequestBody CacnelTransferRequest request) {
        transferService.cancelTransfer(request);
        OperationRepositoryService.saveLog(new SaveLogRequest(
                OperationLog.OperationType.TRANSFER_CANCEL,
                "撤销" + request.getArchiveUnit() + request.getYear() + "年度移交档案",
                "档案移交",
                OperationLog.BusinessType.BUSINESS
        ));
        return ResponseEntity.ok(Result.ok("撤销完成"));
    }

    @ApiOperation("查看差异分析")
    @GetMapping("/showDifference")
    @UserAuthenticate(actionCode = "TRANSFER_DEFFERENCE", skip = true)
    public ResponseEntity<?> showDifference(@RequestParam Long businessId, String archiveUnit, @RequestParam Year year) {
        OperationRepositoryService.saveLog(new SaveLogRequest(
                OperationLog.OperationType.DIFF_ANALYSIS,
                "查看" + (archiveUnit == null?"":archiveUnit) + year + "年度移交档案",
                "档案移交",
                OperationLog.BusinessType.BUSINESS
        ));
        return ResponseEntity.ok(Result.ok(transferService.showDiff(businessId)));
    }
}
